package com.wtwd.device.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.dao.DeviceSettingMapper;
import com.wtwd.device.model.entity.DeviceSetting;
import com.wtwd.device.model.entity.DialManagement;
import com.wtwd.device.model.entity.DialPlate;
import com.wtwd.device.model.entity.UpgradeFile;
import com.wtwd.device.service.DeviceSettingService;
import com.wtwd.device.service.DialPlateService;
import com.wtwd.device.service.DialService;
import com.wtwd.device.service.UpgradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeviceSettingServiceImpl implements DeviceSettingService {
    @Value("${product.image}")
    private String imgPlace;
    @Value("${file.http}")
    private String fileHttp;
    @Autowired
    private DeviceSettingMapper deviceSettingMapper;

    @Resource
    private DialPlateService dialPlateService;

    @Resource
    private DialService dialService;

    @Resource
    private UpgradeService upgradeService;


    @Override
    public Map<String, Object> getDeviceSettingListByWatchId(String watchId, Long userId, Integer curPage, Integer pageSize) {
        Page<DeviceSetting> page = new Page(curPage, pageSize);
        IPage<DeviceSetting> result = deviceSettingMapper.getDeviceSettingListByWatchId(watchId, userId, page);
        List<DeviceSetting> settingList = result.getRecords();
        settingList.stream().forEach(e -> {
            String url = fileHttp.concat(imgPlace.concat(e.getImgName()));
            e.setImgName(url);
        });
        long total = result.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("data", result.getRecords());
        return map;
    }

    @Override
    public Integer addDeviceSetting(DeviceSetting data) {
        return deviceSettingMapper.addDeviceSetting(data);
    }

    @Override
    public List<DeviceSetting> getDeviceListByWatchId(String watchId) {
        return deviceSettingMapper.getDeviceListByWatchId(watchId);
    }

    @Override
    public Integer modifyDeviceSettingByWatchId(DeviceSetting data) {
        return deviceSettingMapper.modifyDeviceSettingByWatchId(data);
    }

    @Override
    public DeviceSetting getDeviceSettingById(Integer id) {
        return deviceSettingMapper.getDeviceSettingById(id);
    }

    @Override
    public List<String> getWatchIdByUserId(Long userId) {
        return deviceSettingMapper.getWatchIdByUserId(userId);
    }

    @Override
    public boolean authWatchIdIsRelevancy(DeviceSetting oldDeviceSetting, String watchId) {
        if (oldDeviceSetting == null) {
            return true;
        }
        if (oldDeviceSetting.getWatchId().equals(watchId)) {
            return false;
        }
        List<DialManagement> dialManagements = dialService.getDialManagementByWatchId(oldDeviceSetting.getWatchId());
        if (dialManagements != null && dialManagements.size() > 0) {
            return true;
        }
        List<DialPlate> dialPlates = dialPlateService.getDialOrderByWatchId(oldDeviceSetting.getWatchId());
        if (dialPlates != null && dialPlates.size() > 0) {
            return true;
        }
        List<UpgradeFile> upgradeFiles = upgradeService.getUpgradesByWatchId(oldDeviceSetting.getWatchId());
        if (upgradeFiles != null && upgradeFiles.size() > 0) {
            return true;
        }
        return false;
    }


    @Override
    public DeviceSetting getDeviceSettingByWatchIdAndUserId(Long userId, String watchId) {
        return deviceSettingMapper.getDeviceSettingByuserIdAndWatchId(userId, watchId);
    }
}
